package com.ctgu.chatapp.view.fragment.MainFragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.ctgu.chatapp.databinding.MsgBoxBinding
import com.ctgu.chatapp.showPojo.FriendMsg
import com.ctgu.chatapp.showPojo.ItemFriend
import com.ctgu.chatapp.utils.Utils
import com.ctgu.chatapp.view.FriendChatDirections
import com.ctgu.chatapp.view.adapter.DiffCallBackForMsg
import com.ctgu.chatapp.view.adapter.MsgBoxAdapter
import com.ctgu.chatapp.viewModel.MainViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MsgBoxFragment : Fragment() {

    private lateinit var controller: Job

    lateinit var binding: MsgBoxBinding

    val viewModel by lazy { ViewModelProvider(requireActivity())[MainViewModel::class.java] }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = MsgBoxBinding.inflate(inflater, container, false)
        binding.viewModel = viewModel
        binding.lifecycleOwner = requireActivity()
        initAdapter()
        return binding.root
    }

    private fun initAdapter() {
        controller = CoroutineScope(IO).launch {
            var oldList = listOf<FriendMsg>()
            val adapter = MsgBoxAdapter(oldList) {
                val viewHolder =
                    binding.msgBox.getChildViewHolder(it) as MsgBoxAdapter.MsgBoxViewHolder
                val record = viewHolder.binding.record!!
                val action = FriendChatDirections.actionGlobalFriendChat(
                    chatID = viewModel.user.value!!.chatID,
                    profile = viewModel.user.value!!.profile,
                    friend = ItemFriend(record.profile, record.friendID, record.nickName)
                )
                Utils.friendID = record.friendID
                println(Utils.friendID)
                view!!.findNavController().navigate(action)
            }
            withContext(Main) {
                binding.msgBox.adapter = adapter
                viewModel.recordList.collect {
                    val result = DiffUtil.calculateDiff(DiffCallBackForMsg(oldList, it), true)
                    result.dispatchUpdatesTo(adapter)
                    oldList = it
                    withContext(Main) {
                        adapter.recordList = oldList
                    }
                }
            }
        }
        binding.msgBox.layoutManager = LinearLayoutManager(requireContext())
    }

    override fun onDestroy() {
        super.onDestroy()
        controller.cancel()
    }
}